package cn.ydxiaoshuai.drools.controller;

import cn.ydxiaoshuai.drools.bean.Address;
import cn.ydxiaoshuai.drools.bean.AddressCheckResult;
import org.drools.core.base.RuleNameStartsWithAgendaFilter;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.rule.FactHandle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 小帅丶
 * @className DroolsController
 * @Description Drools测试Controller
 * @Date 2021-06-10-18:15
 **/
@Controller
@RequestMapping("/drools")
public class DroolsController {
    @Autowired
    private KieSession kieSession;

    @ResponseBody
    @RequestMapping("/address")
    public void test() {
        Address address = new Address();
        address.setPost_code("99425");

//        AddressCheckResult result = new AddressCheckResult();
        kieSession.insert(address);
//        FactHandle f1 = kieSession.insert(result);
        int ruleFiredCount = kieSession.fireAllRules();
        System.out.println("触发了" + ruleFiredCount + "条规则");
//        if (result.isPostCodeResult()) {
//            System.out.println("规则校验通过");
//        }
    }

    @RequestMapping(value = "/test/{postCode}")
    public void test(@PathVariable(value = "postCode")String postCode){
        Address address = new Address();
        address.setPost_code(postCode);

        kieSession.insert(address);

        int fireAllRules = kieSession.fireAllRules();
        System.out.println("触发了" + fireAllRules + "条规则");
        System.out.println("---------------------------------");
    }
}
